package com.film.controller;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.FilmOrder;
import com.film.domain.UserOrder;
import com.film.service.OrderService;
import com.film.service.impl.OrderServiceImpl;

@WebServlet("/order.do")
public class OrderController extends HttpServlet{
	
	private OrderService orderService = new OrderServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String uid = request.getParameter("uid");
		List<FilmOrder> filmOthers = orderService.selectAllOrder(Integer.parseInt(uid));
		List<UserOrder> orderList = orderService.arrangeList(filmOthers);
		if(orderList != null && orderList.size() != 0) {
			orderList.sort(new Comparator<UserOrder>() {
				@Override
				public int compare(UserOrder o1, UserOrder o2) {
					return o2.getDatetime().compareTo(o1.getDatetime());
				}
			});
		}
		session.setAttribute("orderList", orderList);
		response.sendRedirect("order.jsp");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

}
